package com.mall.manager.part.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.WebUtils;

import com.mall.untils.MapUtil;
import com.mall.untils.Page;

/**
 * 
 * @author LISIQIANG
 * 处理角色增、删、改、查业务层
 */
@Service("partService")
public class ManagePartService {
	private Logger logger = LoggerFactory.getLogger(ManagePartService.class);
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    /**
     * 添加角色
     * @param paraMap
     * @return
     */
	public int manager_part_add(Map<String, Object> paraMap){
		
		String part_name = MapUtils.getString(paraMap, "part_name");
		String remarks = MapUtils.getString(paraMap, "remarks");		
		int part_creator = 6;//MapUtils.getIntValue(paraMap, "part_creator");	
					
		try {
			String sql="insert into t_part(part_name,remarks,part_creator,create_time) values(?,?,?,now())";
			int update = this.jdbcTemplate.update(sql,part_name,remarks,part_creator);
			return update;
		} catch (Exception e) {
			logger.info("添加角色失败！");
			return 0;
		}
		
		
	}
	
	

	/**按照Id查找角色的相关信息
	 * @param paraMap
	 * @return
	 */
	public Map<String, Object> manager_part_updateone(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sql="select * from t_part where id=?";
			Map<String, Object> partMap = jdbcTemplate.queryForMap(sql,id);
			return partMap;
		} catch (Exception e) {
			logger.info("查找修改的角色失败！");
			return null;
		}
	}
	
	/**
	 * 角色修改
	 * @param paraMap
	 * @return
	 */
	
	public int manager_part_edit(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");		
		String part_name = MapUtil.getStringValue(paraMap, "part_name");
		String remarks = MapUtil.getStringValue(paraMap, "remarks");		
		int part_editor = 5;MapUtil.getIntValue(paraMap, "part_editor");
		
		String sql="update t_part set part_name=? , remarks=? , part_editor=? ,update_time= now() where id=?";
		try {
			int update = jdbcTemplate.update(sql,part_name,remarks,part_editor,id);
			
			return update;
		} catch (Exception e) {
			logger.info("注册角色信息有问题！");
			return 0;
		}
		
	}
	
	/**
	 * 角色删除
	 * @param paraMap
	 * @return
	 */
	@Transactional
	public int manager_part_delete(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sqlUser="delete from t_user_part where part_id=?";
			int updateUser = jdbcTemplate.update(sqlUser,id);
			if(updateUser==0){
				logger.info("删除与角色关联的用户角色表失败！");
			}
			
			String sqlPower="delete from t_power_part where part_id=?";
			int updatePower = jdbcTemplate.update(sqlPower,id);
			if(updatePower==0){
				logger.info("删除与角色关联的权限角色表失败！");
			}
						
			String sql="delete from t_part where id=?";
			int update = jdbcTemplate.update(sql,id);
			if(update==0){
				logger.info("删除角色表信息失败！");
			}
			
			return update;
		} catch (Exception e) {
			logger.info("删除角色失败！");
			return 0;
		}
	}

	/**
	 * 角色分页查询
	 * @param request
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public Page manager_part_list(HttpServletRequest request) {
		Page page = null; 
	    Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
	    String part_name = MapUtils.getString(paraMap, "part_name");
	    int pageSize = 10;
    	try { 
 	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
 	        
 	       String sql = "select count(*) from t_part ";
 	       
 	       if(part_name != null && !part_name.equals("")){
 	    	  sql += " where part_name = '" + part_name +"'";
 	        } 	        
 	         	       
 	        int total = this.jdbcTemplate.queryForInt(sql);
 	        int startIndex = Page.getStartOfPage(pageNo, pageSize); 	        
 	        sql = "select * from t_part " ;
 	        if(part_name != null && !part_name.equals("")){
  	    	  sql += " where part_name= '" + part_name +"'";
  	        }  	        
 	        sql +=" order by id desc limit " + startIndex + "," + pageSize;
 	        	        
 	        List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(sql);
 	        page = new Page(startIndex, total, pageSize, queryForList);
 	        return page;
 	    } catch (Exception e) {
 	        this.logger.info("获取角色列表失败");
 	        return null;
 	    }
	}
    		
	/**
	 * 角色查询
	 * @param request
	 * @return
	 */
	public List<Map<String, Object>> manager_part_lists(HttpServletRequest request) {
	    Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    String part_name = MapUtils.getString(paraMap, "part_name");
    	try { 
 	       String sql = "";
 	        sql = "select id,part_name from t_part " ;
 	        if(part_name != null && !part_name.equals("")){
  	    	  sql += " where part_name= '" + part_name +"'";
  	        }  	        
 	        sql +=" order by id desc";
 	        	        
 	        List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(sql);
 	        return queryForList;
 	    } catch (Exception e) {
 	        this.logger.info("获取角色列表失败");
 	        return null;
 	    }
	}
        
}
